//
//  NSDate+Extend.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/8.
//

#import "NSDate+Extend.h"
#import "NSDateComponents+Extend.h"

@implementation NSDate (Extend)
-(NSInteger)year{
    NSDateComponents *components = [NSDateComponents rb_dateComponentsFromDate:self];
    return components.year;
}
- (NSInteger) month{
    NSDateComponents *components =  [NSDateComponents rb_dateComponentsFromDate:self];
    return components.month;
}
- (NSInteger) day{
    NSDateComponents *components =  [NSDateComponents rb_dateComponentsFromDate:self];
    return components.day;
}
- (NSInteger) hour{
    NSDateComponents *components =  [NSDateComponents rb_dateComponentsFromDate:self];
    return components.hour;
}
- (NSInteger) minute{
    NSDateComponents *components = [NSDateComponents rb_dateComponentsFromDate:self];
    return components.minute;
}
- (NSInteger) seconds{
    NSDateComponents *components =  [NSDateComponents rb_dateComponentsFromDate:self];
    return components.second;
}
- (NSInteger) weekday{
    NSDateComponents *components =  [NSDateComponents rb_dateComponentsFromDate:self];
    return components.weekday;
}
+(NSDate *)rb_dateWithDateString:(NSString *)dateString{
    NSDate *date = nil;
    date = [self rb_dateWithFormat_yyyy_MM_dd_HH_mm_ss_string:dateString];
    if(date) return date;
    date = [self rb_dateWithFormat_yyyy_MM_dd_HH_mm_string:dateString];
    if(date) return date;
    date = [self rb_dateWithFormat_yyyy_MM_dd_HH_string:dateString];
    if(date) return date;
    date = [self rb_dateWithFormat_yyyy_MM_dd_string:dateString];
    if(date) return date;
    date = [self rb_dateWithFormat_yyyy_MM_string:dateString];
    if(date) return date;
    return nil;
}
+(NSDate *)rb_dateWithFormat_yyyy_MM_dd_HH_mm_ss_string:(NSString *)string{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSLocale *locale = [[NSLocale alloc]  initWithLocaleIdentifier:[[NSLocale preferredLanguages]  objectAtIndex:0]];
    [dateFormat setLocale:locale];
    NSDate *date =[dateFormat dateFromString:string];
    return date;
}
+(NSDate *)rb_dateWithFormat_yyyy_MM_dd_HH_mm_string:(NSString *)string{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSLocale *locale = [[NSLocale alloc]  initWithLocaleIdentifier:[[NSLocale preferredLanguages]  objectAtIndex:0]];
    [dateFormat setLocale:locale];
    NSDate *date =[dateFormat dateFromString:string];
    return date;
}
+(NSDate *)rb_dateWithFormat_yyyy_MM_dd_HH_string:(NSString *)string{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH"];
    NSLocale *locale = [[NSLocale alloc]  initWithLocaleIdentifier:[[NSLocale preferredLanguages]  objectAtIndex:0]];
    [dateFormat setLocale:locale];
    NSDate *date =[dateFormat dateFromString:string];
    return date;
}
+(NSDate *)rb_dateWithFormat_yyyy_MM_dd_string:(NSString *)string{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSLocale *locale = [[NSLocale alloc]  initWithLocaleIdentifier:[[NSLocale preferredLanguages]  objectAtIndex:0]];
    [dateFormat setLocale:locale];
    NSDate *date =[dateFormat dateFromString:string];
    return date;
}
+(NSDate *)rb_dateWithFormat_yyyy_MM_string:(NSString *)string{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM"];
    NSLocale *locale = [[NSLocale alloc]  initWithLocaleIdentifier:[[NSLocale preferredLanguages]  objectAtIndex:0]];
    [dateFormat setLocale:locale];
    NSDate *date =[dateFormat dateFromString:string];
    return date;
}
///按指定格式获取当前的时间
+(NSString *)rb_currentDateStringWithFormat:(NSString *)formatterStr{
    // 获取系统当前时间
    NSDate *currentDate = [NSDate date];
    // 用于格式化NSDate对象
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置格式：yyyy-MM-dd HH:mm:ss
    formatter.dateFormat = formatterStr;
    // 将 NSDate 按 formatter格式 转成 NSString
    NSString *currentDateStr = [formatter stringFromDate:currentDate];
    // 输出currentDateStr
    return currentDateStr;
}
+(NSString *)rb_dayFromFormatWeekday:(NSDate *)date{
    switch([date weekday]) {
        case 1:
            return @"周日";
            break;
        case 2:
            return @"周一";
            break;
        case 3:
            return @"周二";
            break;
        case 4:
            return @"周三";
            break;
        case 5:
            return @"周四";
            break;
        case 6:
            return @"周五";
            break;
        case 7:
            return @"周六";
            break;
        default:
            break;
    }
    return @"";
}
-(NSString *)rb_stringWithFormat:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = format;
    return [formatter stringFromDate:self];
}
@end
